<template>
  <div>
    <el-dialog
      title="提问题"
      :visible.sync="dialogVisible"
      width="700px"
      :lock-scroll="false"
    >
      <div class="right-qa">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="问题描述" prop="content">
            <el-input
              type="textarea"
              v-model="form.content"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="handleSubmit('form')"
          >提 交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        pid: '0', //父级节点(评价类型不传)
        resId: '', //资源ID
        content: '',
        score: 0, //评分
        type: '1', //节点类型；1-评价，2-回复
        resType: '3', //资源类型；1-课程评价，2-知识点评价，3-课程问答，4-知识点问答
      },
      rules: {
        content: [
          { required: true, message: '请输入问题描述', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    this.$bus.$on('showDialogQuestion', () => {
      //判断是否登录
      if (!this.$store.getters.userId) {
        this.loginTip()
        return
      }
      this.dialogVisible = true
    })
  },
  beforeDestroy() {
    this.$bus.$off('showDialogQuestion')
  },
  methods: {
    handleSubmit(form) {
      let { id } = this.$route.query
      this.form.resId = id
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add()
        }
      })
    },
    async add() {
      await this.$store.dispatch('addSysEvaluate', this.form)
      this.dialogVisible = false
      this.form = this.$options.data().form
      this.$bus.$emit('refreshAllQAList')
    },
  },
}
</script>